var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/list', function (req, res, next) {
    res.send('list page')
});

//引入配置和数据库操作
const {
    API_URL
} = require('../utils/config')
const Query = require('../utils/db')

//课程详情
router.get('/api/course', async (req, res, next) => {
    let {
        id = ''
    } = req.query
    if (id == '') {
        next({
            msg: '请输入课程id'
        })
        return;
    }

    let {
        err,
        result: course,
    } = await Query(`select * ,concat('${API_URL}/uploads/courses/',image_src) as image_src,concat('${API_URL}/uploads/courses/',detail_imgs) as detail_imgs from e_course where cid ='${id}'`)

    let {
        result: [teacher],
    } = await Query(`select * ,concat('${API_URL}/uploads/head_photo/',head_photo_url) as head_photo_url  from e_teacher where tid in (select tid from e_course where cid ='${id}')`)

    let {
        result: videos
    } = await Query(`select id,cid,vnum,video_title from e_video where cid = '${id}'`)

    if (err) {
        next({
            msg: "请稍后再试",
        })
        return;
    }
    if (course.length == 0) {
        next({
            msg: "无数据",
        })
        return;
    }

    next({
        msg: "OK",
        status: 200,
        result: {
            course: course[0],
            teacher,
            videos
        }
    })

})
module.exports = router;